package com.group7.mall.service.impl;

import com.group7.mall.convert.OrderConverter;
import com.group7.mall.dao.*;
import com.group7.mall.dto.SellerOrderDetailDto;
import com.group7.mall.dto.SellerOrderPreviewDto;
import com.group7.mall.dto.UserOrderDetailDto;
import com.group7.mall.entity.*;
import com.group7.mall.req.SellerOrderPageRequest;
import com.group7.mall.resp.SellerOrderPageResponse;
import com.group7.mall.service.SellerOrderService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SellerOrderServiceImpl implements SellerOrderService {


    @Resource
    private TbUserOrderDao tbUserOrderDao;

    @Resource
    private TbGoodsDao tbGoodsDao;

    @Resource
    private TbCustomerServiceDao tbCustomerServiceDao;

    @Resource
    private TbSellerDao tbSellerDao;

    @Resource
    private TbUserDao tbUserDao;

    @Resource
    private TbNetProfitDao tbNetProfitDao;

    @Override
    public SellerOrderPageResponse<SellerOrderPreviewDto> showOrderByPage(SellerOrderPageRequest sellerOrderPageRequest) {
        sellerOrderPageRequest.setPageStart((sellerOrderPageRequest.getPageNo() - 1) * sellerOrderPageRequest.getPageSize());
        List<TbUserOrder> tbUserOrderList = tbUserOrderDao.queryStoreOrderByLimit(sellerOrderPageRequest);
        List<SellerOrderPreviewDto> sellerOrderPreviewDtoList = OrderConverter.INSTANCE.convertTbUserOrderListToSellerGoodsPreviewDtoList(tbUserOrderList);
        sellerOrderPreviewDtoList.forEach(sellerOrderPreviewDto -> {
            TbGoods tbGoods = tbGoodsDao.queryOrderGoodsById(sellerOrderPreviewDto.getGoodsId());
            sellerOrderPreviewDto.setGoodsImage(tbGoods.getGoodsImage());
            sellerOrderPreviewDto.setGoodsInfo(tbGoods.getGoodsInfo());
            sellerOrderPreviewDto.setBrand(tbGoods.getBrand());
        });
        SellerOrderPageResponse<SellerOrderPreviewDto> sellerOrderPageResponse = new SellerOrderPageResponse<>();
        sellerOrderPageResponse.setPageSize(sellerOrderPageRequest.getPageSize());
        sellerOrderPageResponse.setCurrent(sellerOrderPageRequest.getPageNo());
        sellerOrderPageResponse.setRecords(sellerOrderPreviewDtoList);
        TbUserOrder tbUserOrder = new TbUserOrder();
        tbUserOrder.setOrderStatus(sellerOrderPageRequest.getOrderStatus());
        tbUserOrder.setStoreId(sellerOrderPageRequest.getStoreId());
        sellerOrderPageResponse.setTotal(tbUserOrderDao.count(tbUserOrder));
        for(SellerOrderPreviewDto x: sellerOrderPreviewDtoList) {
            System.out.println("ok " + x);
        }
        System.out.println(sellerOrderPageResponse.toString());
        return sellerOrderPageResponse;
    }

    @Override
    public SellerOrderDetailDto showOrderDetail(Integer orderId) {
        SellerOrderDetailDto sellerOrderDetailDto = OrderConverter.INSTANCE.convertTbUserOrderToSellerOrderDetailDto(tbUserOrderDao.queryById(orderId));
        TbCustomerService tbCustomerService = tbCustomerServiceDao.queryByOrderId(orderId);
        if (tbCustomerService != null) {
            sellerOrderDetailDto.setServiceId(tbCustomerService.getServiceId());
            sellerOrderDetailDto.setServiceBeginTime(tbCustomerService.getCreatedTime());
            sellerOrderDetailDto.setServiceType(tbCustomerService.getServiceType());
        }
        TbUser tbUser = tbUserDao.queryByUserOpenId(sellerOrderDetailDto.getUserOpenId());
        TbSeller tbSeller = tbSellerDao.queryById(sellerOrderDetailDto.getStoreId());
        TbGoods tbGoods = tbGoodsDao.queryOrderGoodsById(sellerOrderDetailDto.getGoodsId());
        if (tbGoods.getIsDeleted() == 0) {
            sellerOrderDetailDto.setIsDeleted(0);
        } else {
            sellerOrderDetailDto.setIsDeleted(1);
        }
        sellerOrderDetailDto.setStoreName(tbSeller.getStoreName());
        sellerOrderDetailDto.setStoreImage(tbSeller.getStoreImage());
        sellerOrderDetailDto.setUserName(tbUser.getUsername());
        sellerOrderDetailDto.setGoodsClass(tbGoods.getGoodsClass());
        sellerOrderDetailDto.setGoodsImage(tbGoods.getGoodsImage());
        return sellerOrderDetailDto;
    }

    @Override
    public Integer delivery(TbUserOrder tbUserOrder) {
        String expressNumber = tbUserOrder.getExpressNumber();
        tbUserOrder = tbUserOrderDao.queryById(tbUserOrder.getOrderId());
        tbUserOrder.setExpressNumber(expressNumber);
        tbUserOrder.setOrderStatus(2);
        return tbUserOrderDao.update(tbUserOrder);
    }

    @Override
    public Integer serviceFinish(TbUserOrder tbUserOrder) {
        Integer index = tbUserOrder.getIsFinished();
        tbUserOrder = tbUserOrderDao.queryById(tbUserOrder.getOrderId());
        // TODO 订单审核结果
        if (index == 0) {
            // 不通过
            Integer serviceId = tbUserOrder.getServiceId();
            tbCustomerServiceDao.deleteById(serviceId);
            tbUserOrder.setServiceId(null);
            tbUserOrder.setOrderStatus(2);
        } else if (index == 1) {
            tbUserOrder.setIsFinished(1);
            tbNetProfitDao.deleteByOrderId(tbUserOrder.getOrderId());
        }
        return tbUserOrderDao.update(tbUserOrder);
    }

    @Override
    public List<Integer> showOrderCount(Integer storeId) {
        List<Integer> integerList = new ArrayList<>();
        integerList.add(tbUserOrderDao.countStoreOrderByStatus(storeId, null));
        integerList.add(tbUserOrderDao.countStoreOrderByStatus(storeId, 1));
        integerList.add(tbUserOrderDao.countStoreOrderByStatus(storeId, 0));
        integerList.add(tbUserOrderDao.countStoreOrderByStatus(storeId, 2));
        integerList.add(tbUserOrderDao.countStoreOrderByStatus(storeId, 4));
        integerList.add(tbUserOrderDao.queryTransactionVolume(storeId));
        return integerList;
    }

    @Override
    public List<Double> showOrderPrice(@RequestBody TbSeller tbSeller) {
        Integer storeId = tbSeller.getStoreId();
        List<Double> list = new ArrayList<>();
        List<TbUserOrder> tbUserOrders12 = tbUserOrderDao.queryAfterCreatedTime(1, tbSeller.getCreatedTime(), storeId);
        List<TbUserOrder> tbUserOrders3 = tbUserOrderDao.queryAfterCreatedTime(3, tbSeller.getCreatedTime(), storeId);
        List<TbUserOrder> tbUserOrders4 = tbUserOrderDao.queryAfterCreatedTime(4, tbSeller.getCreatedTime(), storeId);
        Double i = 0.0;
        for(TbUserOrder x: tbUserOrders12) {
            i += x.getOrderPrice();
        }
        list.add(i);
        i = 0.0;
        for(TbUserOrder x: tbUserOrders3) {
            i += x.getOrderPrice();
        }
        list.add(i);
        i = 0.0;
        for(TbUserOrder x: tbUserOrders4) {
            i += x.getOrderPrice();
        }
        list.add(i);
        return list;
    }
}
